//
//  TMGChangePwdViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/8.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGRansomViewController.h"


@interface TMGRansomViewController ()
@property(nonatomic, strong) UITextField                     *moneyTextField;
@property(nonatomic, strong) NSArray<STTableEasyModel*>                     *dataSource;
@end

@implementation TMGRansomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"赎回零钱计划";
    [self configSubView];
    [self configFooterView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    self.moneyTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth * 0.66, 20)];
    self.moneyTextField.placeholder = @"请输入赎回金额";
    self.moneyTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    
    self.dataSource = @[
                        [[STTableEasyModel alloc] initWithTextString:@"赎回金额" accessoryView:self.moneyTextField]];
    [self.tableView reloadData];
    
}
- (void)configFooterView{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];

    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60 , 44)
                                                        title:@"确认赎回"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:10
                                              backgroundColor:TM_orangeColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedRensomButton];
    }];
    confimButton.top = 50;
    
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    self.tableView.tableFooterView = footer;
}

#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 44;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * sectionheader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(self.insetX, 0, UIScreenWidth - 30, 44)
                                                     text:[NSString stringWithFormat:@"可赎回金额%@元",
                                                           self.money]
                                                textColor:TM_firstTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    titleLable.attributedText = [titleLable.text st_convertAttributeStringWithKeyWord:self.money attributes:@{NSForegroundColorAttributeName:TM_orangeColor}];
    [sectionheader addSubview:titleLable];
    return sectionheader;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    STTableEasyModel * model = self.dataSource[indexPath.row];
    cell.textLabel.text = model.textString;
    cell.detailTextLabel.text = model.detailString;
    if (model.accessoryView) {
        cell.accessoryView = model.accessoryView;
    }else{
        cell.accessoryType = model.accessoryType;
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelectedRensomButton{
    if (!self.moneyTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入赎回金额"];
        return;
    }
    [self presentInputPwdViewController];
}
- (void)presentInputPwdViewController{
    LBPassWordPresentViewController * vc = [LBPassWordPresentViewController new];
    [vc setPwdDidEdited:^(LBPassWordPresentViewController *passVC ,NSString *inputPwd) {
        
        if (inputPwd.length) {
            [self sendSaleGoldAssetRequestWithPwd:inputPwd handle:^(NSString *code) {
                if (!code.length) {
                    [SVProgressHUD showSuccessWithStatus:@"赎回成功"];
                    [passVC dismissWithHandle:^{
                        [self.navigationController popToRootViewControllerAnimated:YES];
                    }];
                }else{
                    if ([code containsString:@"密码"]) {
                        passVC.alertLable.hidden = NO;
                        [passVC rightLablebackAnimation];
                    }

                }
            }];
        }
        
    }];
    [self presentViewController:vc animated:NO completion:nil];
}
- (void)sendSaleGoldAssetRequestWithPwd:(NSString*)pwd handle:(void(^)(NSString * code))handle{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    if (pwd.length) {
        [dic setObject:pwd forKey:@"pay_password"];
    }
    if (self.moneyTextField.text.length) {
        [dic setObject:self.moneyTextField.text forKey:@"goods_number"];
    }
    [dic setObject:@"4" forKey:@"trade_type"];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/order/addOrderSell"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 if (handle) {
                                                     handle(@"");
                                                 }
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 if (handle) {
                                                     handle(error.desc);
                                                 }
                                             }];
}

@end


